{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reformat Phone Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reformatNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #重新格式化电话号码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串形式的电话号码 <code>number</code> 。<code>number</code> 由数字、空格 <code>' '</code>、和破折号 <code>'-'</code> 组成。</p>\n",
    "\n",
    "<p>请你按下述方式重新格式化电话号码。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>首先，<strong>删除</strong> 所有的空格和破折号。</li>\n",
    "\t<li>其次，将数组从左到右 <strong>每 3 个一组</strong> 分块，<strong>直到 </strong>剩下 4 个或更少数字。剩下的数字将按下述规定再分块：\n",
    "\t<ul>\n",
    "\t\t<li>2 个数字：单个含 2 个数字的块。</li>\n",
    "\t\t<li>3 个数字：单个含 3 个数字的块。</li>\n",
    "\t\t<li>4 个数字：两个分别含 2 个数字的块。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "\n",
    "<p>最后用破折号将这些块连接起来。注意，重新格式化过程中 <strong>不应该</strong> 生成仅含 1 个数字的块，并且 <strong>最多</strong> 生成两个含 2 个数字的块。</p>\n",
    "\n",
    "<p>返回格式化后的电话号码。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"1-23-45 6\"\n",
    "<strong>输出：</strong>\"123-456\"\n",
    "<strong>解释：</strong>数字是 \"123456\"\n",
    "步骤 1：共有超过 4 个数字，所以先取 3 个数字分为一组。第 1 个块是 \"123\" 。\n",
    "步骤 2：剩下 3 个数字，将它们放入单个含 3 个数字的块。第 2 个块是 \"456\" 。\n",
    "连接这些块后得到 \"123-456\" 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"123 4-567\"\n",
    "<strong>输出：</strong>\"123-45-67\"\n",
    "<strong>解释：</strong>数字是 \"1234567\".\n",
    "步骤 1：共有超过 4 个数字，所以先取 3 个数字分为一组。第 1 个块是 \"123\" 。\n",
    "步骤 2：剩下 4 个数字，所以将它们分成两个含 2 个数字的块。这 2 块分别是 \"45\" 和 \"67\" 。\n",
    "连接这些块后得到 \"123-45-67\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"123 4-5678\"\n",
    "<strong>输出：</strong>\"123-456-78\"\n",
    "<strong>解释：</strong>数字是 \"12345678\" 。\n",
    "步骤 1：第 1 个块 \"123\" 。\n",
    "步骤 2：第 2 个块 \"456\" 。\n",
    "步骤 3：剩下 2 个数字，将它们放入单个含 2 个数字的块。第 3 个块是 \"78\" 。\n",
    "连接这些块后得到 \"123-456-78\" 。</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"12\"\n",
    "<strong>输出：</strong>\"12\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 5：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"--17-5 229 35-39475 \"\n",
    "<strong>输出：</strong>\"175-229-353-94-75\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 <= number.length <= 100</code></li>\n",
    "\t<li><code>number</code> 由数字和字符 <code>'-'</code> 及 <code>' '</code> 组成。</li>\n",
    "\t<li><code>number</code> 中至少含 <strong>2</strong> 个数字。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reformat-phone-number](https://leetcode.cn/problems/reformat-phone-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reformat-phone-number](https://leetcode.cn/problems/reformat-phone-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"1-23-45 6\"', '\"123 4-567\"', '\"123 4-5678\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = ''.join([x for x in number if x.isdigit()])\n",
    "        idx,l,ans = 0,len(s),[]\n",
    "        while True:\n",
    "            if l - idx > 4:\n",
    "                ans.append(s[idx:idx+3])\n",
    "                idx += 3\n",
    "            else:\n",
    "                if l - idx == 4:\n",
    "                    ans.append(s[idx:idx+2])\n",
    "                    ans.append(s[idx+2:])\n",
    "                else:\n",
    "                    ans.append(s[idx:])\n",
    "                return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        n,pt = len(digits),0\n",
    "        ans = list()\n",
    "\n",
    "        while n>0:\n",
    "            if n>4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt +=3\n",
    "                n -=3\n",
    "            else:\n",
    "                if n==4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = \"\".join((\"\".join(number.split())).split('-'))\n",
    "        print(number)\n",
    "        l = len(number)\n",
    "        if l <= 3: return number\n",
    "        tail = \"\"\n",
    "        if l % 3:\n",
    "            if l % 3 == 1:\n",
    "                tail = number[-4:-2] + \"-\" + number[-2:]\n",
    "                number = number[:-4]\n",
    "            else:\n",
    "                tail = number[-2:]\n",
    "                number = number[:-2]\n",
    "                print(\"tail\", tail, \"number\", number)\n",
    "        ans = \"\"\n",
    "        for i in range(0, len(number), 3):\n",
    "            ans += number[i : i + 3] + \"-\"\n",
    "        return ans + tail if tail != \"\" else ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\",\"\")\n",
    "        limit = len(number)\n",
    "        n = limit%3\n",
    "        ans = [number[i:i+3] for i in range(0,limit-2,3)]\n",
    "        if n == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        if n == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        a=\"\"\n",
    "        n=len(number)\n",
    "        an=[number[i*3:i*3+3]\n",
    "        for i in range(n//3)]\n",
    "        if n%3==1:\n",
    "            an[-1]=an[-1][:2]\n",
    "            an.append(number[-2:])\n",
    "        if n%3==2:\n",
    "            an.append(number[-2:])\n",
    "        for i in range(len(an)):\n",
    "            a+=(an[i] + \"-\")\n",
    "        return a[0:-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        str1 = ''\n",
    "        component = []\n",
    "        for i in number:\n",
    "            if i.isdigit():\n",
    "                str1 += i                \n",
    "        while (len(str1)) > 4:\n",
    "            component.append(str1[:3])\n",
    "            str1 = str1[3:]\n",
    "        if (len(str1)) == 4:\n",
    "            component.append(str1[:2])\n",
    "            component.append(str1[2:])\n",
    "        else:\n",
    "            component.append(str1[:])\n",
    "        return '-'.join(component)\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        n, idx = len(s), 0\n",
    "        res = \"\"\n",
    "        while idx < n:\n",
    "            if res:\n",
    "                res += \"-\"\n",
    "            if idx + 5 > n:\n",
    "                res += s[idx:] if idx + 3 >= n else f\"{s[idx:idx+2]}-{s[idx+2:]}\"\n",
    "                break\n",
    "            res += s[idx:idx+3]\n",
    "            idx += 3\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number= number.replace(\"-\",\"\").replace(\" \",\"\")\n",
    "        s= \"\"\n",
    "        while(True):\n",
    "            n = len(number)\n",
    "            if n <=3:\n",
    "                return s+number\n",
    "            elif n==4:\n",
    "                return s+number[:2]+'-'+number[2:]\n",
    "            else:\n",
    "                s = s + number[:3]+\"-\"\n",
    "                number = number[3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace('-','').replace(' ','')\n",
    "        ans = [number[i : i + 3] for i in range(0, len(number), 3)]\n",
    "        if len(ans[-1]) == 1:\n",
    "            ans[-1] = ans[-2][-1] + ans[-1]\n",
    "            ans[-2] = ans[-2][:2]\n",
    "        return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        clean_str = re.sub(' |-', '', number)\n",
    "        \n",
    "        n = len(clean_str)//3\n",
    "        remainder = len(clean_str)%3\n",
    "        ans = []\n",
    "        for i in range(n-1):\n",
    "            ans.append(clean_str[3*i:3*(i+1)])\n",
    "        if remainder == 1:\n",
    "            ans.append(clean_str[-4:-2])\n",
    "            ans.append(clean_str[-2:])\n",
    "            \n",
    "        elif remainder == 2:\n",
    "            if n:\n",
    "                ans.append(clean_str[-5:-2])\n",
    "                ans.append(clean_str[-2:])\n",
    "            else:\n",
    "                ans.append(clean_str[-2:])\n",
    "        else:\n",
    "            ans.append(clean_str[-3:])\n",
    "        \n",
    "        # ans = [i for i in ans if i]\n",
    "        return '-'.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        \n",
    "        n, pt = len(digits), 0\n",
    "        ans = list()\n",
    "\n",
    "        while n > 0:\n",
    "            if n > 4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt += 3\n",
    "                n -= 3\n",
    "            else:\n",
    "                if n == 4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\")\n",
    "        number = number.replace(\"-\", \"\")\n",
    "        num_len = len(number)\n",
    "        res = []\n",
    "        for index, s in enumerate(number):\n",
    "            if index % 3 == 0 and index != 0:\n",
    "                tmp = number[index-3: index]\n",
    "                res.append(tmp)\n",
    "        res.append(number[(index//3)*3:])\n",
    "        if len(res[-1]) == 1:\n",
    "            res[-1] = res[-2][-1] + res[-1]\n",
    "            res[-2] = res[-2][:-1]\n",
    "        \n",
    "        result = \"\"\n",
    "        for i in res:\n",
    "            result += i + \"-\"\n",
    "        return result[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = re.sub(r'[-\\s]', '', number)\n",
    "        ans = ''\n",
    "        m = len(number)\n",
    "        n = 0\n",
    "        while n < m:\n",
    "            if m-n == 4:\n",
    "                ans += number[n:n+2] + '-' + number[n + 2:n + 4]\n",
    "                break\n",
    "            elif m-n == 2:\n",
    "                ans += number[n:n+2]\n",
    "                break\n",
    "            elif m - n == 3:\n",
    "                ans += number[n:n+3]\n",
    "                break\n",
    "            else:\n",
    "                ans += number[n:n+3] + '-'\n",
    "                n+= 3\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace(\"-\",\"\").replace(\" \",\"\")\n",
    "        n=len(number)\n",
    "        ans=[number[3*i:3*i+3] for i in range(n//3)]\n",
    "        if n%3==1:\n",
    "            ans[-1]=ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n%3==2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ','')\n",
    "        number = number.replace('-','')\n",
    "        print(number)\n",
    "        ans = []\n",
    "        m = len(number)\n",
    "        n = 0\n",
    "        while n < m:\n",
    "            if m-n == 4:\n",
    "                ans.append(number[n:n+2])\n",
    "                n += 2\n",
    "                ans.append(number[n:n+2]) \n",
    "                n= m\n",
    "            elif m-n == 2:\n",
    "                ans.append(number[n:n+2]) \n",
    "                n = m\n",
    "            else:\n",
    "                ans.append(number[n:n+3])\n",
    "                n+= 3\n",
    "        print(ans)\n",
    "        return '-'.join(ans) \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace('-','').replace(' ','')\n",
    "        print(number)\n",
    "        arr=str(number)\n",
    "        n=len(arr)\n",
    "        # ans = [number[i * 3: i * 3 + 3] for i in range(n // 3)]\n",
    "        res=[]\n",
    "        while len(number)>4:\n",
    "            res.append(number[:3])\n",
    "            number=number[3:]\n",
    "        if len(number)==4:\n",
    "            res.append(number[:2])\n",
    "            res.append(number[2:])\n",
    "        else:\n",
    "            res.append(number[:])\n",
    "        return '-'.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        result = []\n",
    "        count = 0\n",
    "        number = number.replace(\"-\",\"\").replace(\" \",\"\") #去除空格和破折号\n",
    "        for c in number:\n",
    "            count+=1\n",
    "            result.append(c)\n",
    "            if count % 3 == 0:\n",
    "                result.append('-')\n",
    "        if count % 3 == 0:\n",
    "            result.pop()\n",
    "        elif count % 3 == 1:\n",
    "            c = result[-3]\n",
    "            result[-3] = '-'\n",
    "            result[-2] = c\n",
    "        return \"\".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        a =\"\"\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] \n",
    "        for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        if n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        for i in range(len(ans)):\n",
    "            a+=(ans[i] + \"-\")\n",
    "        return a[0:-1]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ', '').replace('-', '')\n",
    "        n = len(number)\n",
    "        res = [number[3*i:3*(i+1)] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            res[-1] = res[-1][:2]\n",
    "            res.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            res.append(number[-2:])\n",
    "        \n",
    "        return '-'.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = ''.join(ch for ch in number if ch.isalnum())\n",
    "        i = 0\n",
    "        res = ''\n",
    "        while len(s[i:]) > 4:\n",
    "            res = res + s[i:i+3] +'-'\n",
    "            i += 3\n",
    "    \n",
    "        if len(s[i:]) == 4:\n",
    "            res = res + s[i:i+2] +'-'\n",
    "            res += s[i+2:]\n",
    "        else:\n",
    "            res += s[i:]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 先去除空格和破折号\n",
    "        numbers = number.replace('-', '').replace(' ', '')\n",
    "        l = len(numbers)\n",
    "        \n",
    "        def C(con, num):\n",
    "            lg = len(num)\n",
    "            ls = ''\n",
    "            count = 0\n",
    "            for i in range(lg):\n",
    "                count += 1\n",
    "                ls += num[i]\n",
    "                if count == con and i != lg - 1:\n",
    "                    ls += '-'\n",
    "                    count = 0\n",
    "            return ls\n",
    "        # 根据number的长度来判断选用\n",
    "        if l <= 3:\n",
    "            return numbers\n",
    "        if l % 3 == 0:\n",
    "            return C(3, numbers)\n",
    "        elif l % 3 == 1:\n",
    "            if l==4:\n",
    "                return C(2, numbers[l - 4:])\n",
    "            else:\n",
    "                return C(3, numbers[0:l - 4]) +'-'+ C(2, numbers[l - 4:])\n",
    "        elif l % 3 == 2:\n",
    "            return C(3, numbers[0:l - 2] + C(2, numbers[l - 2:]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        res, tmp = '', 0\n",
    "        ls = list(number)\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i] == '-' or ls[i] == ' ':\n",
    "                ls[i] = ''\n",
    "            else:\n",
    "                tmp += 1\n",
    "                res += ls[i]\n",
    "                if tmp == 3:\n",
    "                    res += '-'\n",
    "                    tmp = 0\n",
    "        if tmp == 0:\n",
    "            res = res[:-1]\n",
    "        elif tmp == 1:\n",
    "            res = res[:-3] + '-' + res[-3] + res[-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        res, tmp = '', 0\n",
    "        ls = list(number)\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i] == '-' or ls[i] == ' ':\n",
    "                ls[i] = ''\n",
    "            else:\n",
    "                tmp += 1\n",
    "                res += ls[i]\n",
    "                if tmp == 3:\n",
    "                    res += '-'\n",
    "                    tmp = 0\n",
    "        if tmp == 0:\n",
    "            res = res[:-1]\n",
    "        elif tmp == 1:\n",
    "            res = res[:-3] + '-' + res[-3] + res[-1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 删除所有的空格和破折号  \n",
    "        number = ''.join(c for c in number if c.isdigit())  \n",
    "    \n",
    "        # 将数组从左到右每3个一组分块，直到剩下4个或更少数字  \n",
    "        chunks = [number[i:i+3] for i in range(0, len(number), 3)]  \n",
    "        k = len(chunks[-1]) % 3\n",
    "        # 根据规定再分块  \n",
    "        result = '-'.join(chunks)\n",
    "        return result[:-3] + \"-\" + result[-3] + result[-1] if k == 1 else result[:]  # 去掉第一个额外的破折号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 删除所有的空格和破折号  \n",
    "        number = ''.join(c for c in number if c.isdigit())  \n",
    "    \n",
    "        # 将数组从左到右每3个一组分块，直到剩下4个或更少数字  \n",
    "        chunks = [number[i:i+3] for i in range(0, len(number), 3)]  \n",
    "        k = len(chunks[-1]) % 3\n",
    "        # 根据规定再分块  \n",
    "        result = '-'.join(chunks)\n",
    "        return result[:-3] + \"-\" + result[-3] + result[-1] if k == 1 else result[:]  # 去掉第一个额外的破折号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "\n",
    "\n",
    "\n",
    "        newNum = \"\"\n",
    "\n",
    "\n",
    "        for num in number:\n",
    "\n",
    "\n",
    "            if num.isdigit():\n",
    "                newNum += num\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        res = \"\"\n",
    "\n",
    "        n = len(newNum)\n",
    "        print(newNum)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            left = n - i \n",
    "            if left > 4:\n",
    "                res += newNum[i: i + 3] + \"-\"\n",
    "            else:\n",
    "                break\n",
    "            i += 3\n",
    "        print(left)\n",
    "        if left == 2 or left == 3:\n",
    "            res += newNum[i:]\n",
    "        elif left == 4:\n",
    "            res +=  newNum[i: i + 2] + \"-\" + newNum[i + 2:]\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# code 1\n",
    "# import re\n",
    "# class Solution:\n",
    "#     def reformatNumber(self, number: str) -> str:\n",
    "#         self.number = number\n",
    "#         num = str(re.sub(' |-', '',self.number))\n",
    "#         lst = []\n",
    "\n",
    "#         while len(num) > 4:\n",
    "#             lst.append(num[0:3])\n",
    "#             num = num[3:]\n",
    "#         if len(num) == 4:\n",
    "#             lst.append(num[0:2])\n",
    "#             lst.append(num[2:])\n",
    "#         else:\n",
    "#             lst.append(num)\n",
    "        \n",
    "#         result = '-'.join(lst)\n",
    "#         return result\n",
    "\n",
    "\n",
    "# code 2\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for char in number:\n",
    "            if char.isdigit():\n",
    "                digits.append(char)\n",
    "                \n",
    "        n,pt = len(digits),0\n",
    "        ans = list()\n",
    "\n",
    "        while n>0:\n",
    "            if n>4:\n",
    "                ans.append(''.join(digits[pt:pt+3]))\n",
    "                pt+=3\n",
    "                n-=3\n",
    "            else:\n",
    "                if n==4:\n",
    "                    ans.append(''.join(digits[pt:pt+2]))\n",
    "                    ans.append(''.join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(''.join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        arr=[]\n",
    "        for i in number:\n",
    "            if i!=' ' and i!='-':\n",
    "                arr.append(i)\n",
    "        n=len(arr)\n",
    "        ans=''\n",
    "        p=''\n",
    "        if n%3==1:\n",
    "            p=arr[-4]+arr[-3]+'-'+arr[-2]+arr[-1]+'-'\n",
    "            n-=4\n",
    "        elif n%3==2:\n",
    "            p=arr[-2]+arr[-1]+'-'\n",
    "            n-=2\n",
    "        i=0\n",
    "        while i<n:\n",
    "            ans+=arr[i]+arr[i+1]+arr[i+2]\n",
    "            ans+='-'\n",
    "            i+=3\n",
    "        ans+=p\n",
    "        return ans[:-1]\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        ans = ''\n",
    "        pos = 0\n",
    "        total = sum(1 for x in number if x !='-' and x !=' ')\n",
    "        need = 0\n",
    "        if total % 3 ==0:\n",
    "            need = total-3\n",
    "        else:\n",
    "            need = total-2\n",
    "        for i in range(len(number)):\n",
    "            if number[i] != '-' and number[i] !=' ':\n",
    "                ans+=number[i]\n",
    "                pos+=1\n",
    "                if (pos<need and pos % 3==0) or pos ==need:\n",
    "                    ans += '-'\n",
    "        \n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        lst = [x for x in number if x.isnumeric()]\n",
    "        ans = []\n",
    "        while len(lst) > 4:\n",
    "            ans.append(\"\".join(lst[:3]))\n",
    "            lst = lst[3:]\n",
    "        if len(lst) <= 3:\n",
    "            ans.append(\"\".join(lst))\n",
    "        else:\n",
    "            ans.append(\"\".join(lst[:2]))\n",
    "            ans.append(\"\".join(lst[2:]))\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        s = ''.join([x for x in number if x.isdigit()])\n",
    "        idx,l,ans = 0,len(s),[]\n",
    "        while True:\n",
    "            if l - idx > 4:\n",
    "                ans.append(s[idx:idx+3])\n",
    "                idx += 3\n",
    "            else:\n",
    "                if l - idx == 4:\n",
    "                    ans.append(s[idx:idx+2])\n",
    "                    ans.append(s[idx+2:])\n",
    "                else:\n",
    "                    ans.append(s[idx:])\n",
    "                return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        nstr = number.replace(' ', '').replace('-', '')\n",
    "        n = len(nstr)\n",
    "        ans = ''\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if n - i > 4:\n",
    "                ans += nstr[i: i+3] + '-'\n",
    "                i += 3\n",
    "            if n - i == 4:\n",
    "                ans += nstr[i: i+2] + '-' + nstr[i + 2:]\n",
    "                break\n",
    "            if n - i <=3:\n",
    "                ans += nstr[i:]\n",
    "                break\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        ans = ''\n",
    "        for i in range(len(number)):\n",
    "            if number[i].isdigit():\n",
    "                ans += number[i]\n",
    "        n = len(ans)\n",
    "        if n<=3:return ans\n",
    "        m , residual = (n-4) //3 ,(n-4) % 3\n",
    "        res = ''\n",
    "        for i in range(m):\n",
    "            res = res + ans[i*3 : (i+1)*3] + '-'\n",
    "        if residual == 0:\n",
    "            res = res + ans[-4:-2] + '-' + ans[-2:]\n",
    "        elif residual == 1:\n",
    "            res = res + ans[-5:-2] + '-' + ans[-2:]\n",
    "        else:\n",
    "            res = res + ans[-6:-3] + '-' + ans[-3:]\n",
    "        return res\n",
    "        \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \",\"\").replace(\"-\",\"\")#进行删除处理\n",
    "        s = \"\"\n",
    "        for x in range(0,len(number),3):\n",
    "            if x == len(number) - 4: #考虑最后出现剩余4个数字的情况\n",
    "                s += number[x:x+2] + \"-\" + number[x+2:]\n",
    "                break\n",
    "            elif x == len(number) - 2:\n",
    "                s += number[x:x+2]\n",
    "                break\n",
    "            else:\n",
    "                s += number[x:x+3] + \"-\" #进行分组处理\n",
    "        if s[-1] == \"-\" :\n",
    "            return s[:-1] #解决最后末尾出现的\"-\"\n",
    "        else:\n",
    "            return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        \n",
    "        temp = []\n",
    "        for c in number:\n",
    "            if c in \"0123456789\":\n",
    "                temp.append(c)\n",
    "                \n",
    "        #n = len(temp)\n",
    "        ans = \"\"\n",
    "        count = 0\n",
    "        while temp:\n",
    "            if len(temp) == 4:\n",
    "                ans += temp[0]\n",
    "                ans += temp[1]\n",
    "                ans += '-'\n",
    "                ans += temp[2]\n",
    "                ans += temp[3]\n",
    "                break\n",
    "            elif len(temp) < 4:\n",
    "                for i in temp:\n",
    "                    ans += i\n",
    "                break\n",
    "            c = \"\"\n",
    "            for i in range(3):\n",
    "                try:\n",
    "                    c += temp.pop(0)\n",
    "                    count += 1\n",
    "                except:\n",
    "                    break\n",
    "            ans += c\n",
    "            if count % 3 == 0:\n",
    "                ans += '-'\n",
    "        return ans\n",
    "            \n",
    "        '''\n",
    "        res = \"\"\n",
    "        number1 = \"\"\n",
    "        count = 0\n",
    "        while count < len(number):\n",
    "            if number[count] != \" \" and number[count] != \"-\":\n",
    "                number1 += number[count]\n",
    "            count += 1\n",
    "        count = 0\n",
    "        while count < len(number1):\n",
    "            if count + 1 % 3 == 0 and count != 0:\n",
    "                res += \"-\"\n",
    "            res += number1[count]\n",
    "            count += 1\n",
    "            if len(number1) - count <= 4:\n",
    "                if len(number1) - count == 2:\n",
    "                    res += \"-\"\n",
    "                    res += number1[-2]\n",
    "                    res += number1[-1]\n",
    "                if len(number1) - count == 3:\n",
    "                    res += \"-\"\n",
    "                    res += number1[-3]\n",
    "                    res += number1[-2]\n",
    "                    res += number1[-1]\n",
    "                if len(number1) - count == 4:\n",
    "                    res += \"-\"\n",
    "                    res += number1[-4]\n",
    "                    res += number1[-3]\n",
    "                    res += \"-\"\n",
    "                    res += number1[-2]\n",
    "                    res += number1[-1]\n",
    "                break\n",
    "        return res\n",
    "        '''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        nums = ''.join(''.join(number.split()).split('-'))\n",
    "        #print(nums)\n",
    "        i = 0\n",
    "        n = len(nums)\n",
    "        res = ''\n",
    "        while i < n - 1 and i + 4 < n:\n",
    "            res = res + nums[i: i + 3] + '-'\n",
    "            i += 3\n",
    "        print(res)\n",
    "        m = len(nums[i:])\n",
    "        if m == 2 or m == 3:\n",
    "            res += nums[i:]\n",
    "        else:\n",
    "            res = res + nums[i: i + 2] + '-' + nums[i + 2:]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        fh = []\n",
    "        for i in number:\n",
    "            if i.isdigit():\n",
    "                fh.append(i)\n",
    "        n = len(fh)\n",
    "        if n%3 == 1:\n",
    "            if n == 4:\n",
    "                return \"\".join(fh[-4:-2]) + \"-\" + \"\".join(fh[-2:])\n",
    "            return \"-\".join(\"\".join(fh[i:i+3]) for i in range(0, n-4,3)) + \"-\" + \"\".join(fh[-4:-2]) + \"-\" + \"\".join(fh[-2:])\n",
    "        else:\n",
    "            return \"-\".join(\"\".join(fh[i:i+3]) for i in range(0, n, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        n = len(number)\n",
    "        if n % 3 == 1:\n",
    "            m = n - 4\n",
    "            s = number[m: m + 2] + \"-\" + number[m + 2:]\n",
    "        else:\n",
    "            m = n - n % 3\n",
    "            s = number[m:]\n",
    "        ans = \"\"\n",
    "        for i in range(0, m, 3):\n",
    "            ans += number[i: i + 3] + \"-\"\n",
    "        ans = ans + s\n",
    "        return ans if n % 3 != 0 else ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        a=number.replace('-','')\n",
    "        b=a.replace(' ','')   \n",
    "        print(b)  \n",
    "        res=[]           \n",
    "        for i in range(0,len(b),3):\n",
    "            res.append(b[i:i+3])    \n",
    "        if len(res[-1])==1 and len(res)>1:\n",
    "            t=res[-2]+res[-1]\n",
    "            res[-2],res[-1]=t[:2],t[2:]\n",
    "        \n",
    "        return '-'.join(res)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace(' ','').replace('-','')\n",
    "        num=0\n",
    "        res=''\n",
    "        for i in number:\n",
    "            res+=i\n",
    "            num+=1\n",
    "            if num==3:\n",
    "                res+='-'\n",
    "                num=0\n",
    "        if res[-1]=='-':res=res[:-1]\n",
    "        if res[-2]=='-':res=res[:-3]+'-'+res[-3]+res[-1]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        \n",
    "        n, pt = len(digits), 0\n",
    "        ans = list()\n",
    "\n",
    "        while n > 0:\n",
    "            if n > 4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt += 3\n",
    "                n -= 3\n",
    "            else:\n",
    "                if n == 4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        temp = \"\"\n",
    "        for num in number:\n",
    "            if num != \" \" and num != \"-\":\n",
    "                temp += num\n",
    "        length = len(temp)\n",
    "        res = \"\"\n",
    "        start = 0\n",
    "        while length>4:\n",
    "            res += temp[start:start+3]+\"-\"\n",
    "            start += 3\n",
    "            length -= 3\n",
    "        if length == 4:\n",
    "            res += temp[start:start+2]+\"-\"+temp[start+2:start+4]\n",
    "        elif length == 3:\n",
    "            res += temp[start:start+3]\n",
    "        elif length == 2:\n",
    "            res += temp[start:start+2]\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = re.compile(r\"[- ]\").sub(\"\", number)\n",
    "        idx, n, ans = 0, len(number), []\n",
    "        while idx + 4 < n:\n",
    "            ans.append(number[idx : idx + 3])\n",
    "            idx += 3\n",
    "        if n - idx == 4:\n",
    "            ans.append(number[idx : idx + 2])\n",
    "            idx += 2\n",
    "        ans.append(number[idx:n])\n",
    "        return \"-\".join(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \", \"\").replace(\"-\", \"\")\n",
    "        n = len(number)\n",
    "        if n % 3 == 1:\n",
    "            if n > 3:\n",
    "                m = n - 4\n",
    "                s = number[m: m + 2] + \"-\" + number[m + 2:]\n",
    "            else:\n",
    "                m = 1\n",
    "                s = number\n",
    "        else:\n",
    "            m = n - n % 3\n",
    "            if n % 3 != 0:\n",
    "                s = number[m:]\n",
    "            else:\n",
    "                s = \"\"\n",
    "        lst = []\n",
    "        for i in range(0, m, 3):\n",
    "            lst.append(number[i: i + 3])\n",
    "        lst.append(s)\n",
    "        ans = \"-\".join(lst)\n",
    "        return ans if n % 3 != 0 else ans[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ','').replace('-','')\n",
    "        components = []\n",
    "\n",
    "        while len(number) > 4:\n",
    "            components.append(number[:3])\n",
    "            number = number[3:]\n",
    "\n",
    "        if len(number) == 4:\n",
    "            components.append(number[:2])\n",
    "            components.append(number[2:])\n",
    "        else:\n",
    "            components.append(number[:])\n",
    "\n",
    "        return '-'.join(components)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(' ','').replace('-','')\n",
    "        n = len(number)\n",
    "        iter = n // 3\n",
    "        if n <=3:\n",
    "            return number\n",
    "        elif n % 3 == 0:\n",
    "            str = ''\n",
    "            for i in range(iter):\n",
    "                str += number[3*i:3*i+3] + '-'\n",
    "            number = str[:-1]\n",
    "            return number\n",
    "        elif n % 3 == 1:\n",
    "            str = ''\n",
    "            for i in range(iter-1):\n",
    "                str += number[3*i:3*i+3] + '-'\n",
    "            number = str + number[-4:-2] + '-' + number[-2:]\n",
    "            return number\n",
    "        else:\n",
    "            str = ''\n",
    "            for i in range(iter):\n",
    "                str += number[3*i:3*i+3] + '-'\n",
    "            number = str + number[-2:]\n",
    "            return number\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace('-','')\n",
    "        number=number.replace(' ','')\n",
    "        ans=''\n",
    "        n=len(number)\n",
    "        if n%3==1:\n",
    "            n=n-4\n",
    "        for i in range(0,n,3):\n",
    "            ans+=number[i:i+3]+\"-\"\n",
    "        if len(number)!=n:\n",
    "            ans=ans+number[n:n+2]+\"-\"+number[n+2:]\n",
    "        if ans[-1]=='-':\n",
    "            ans=ans[:-1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        x = \"\"\n",
    "        target = \"\"\n",
    "        for i in range (len(number)):\n",
    "            if number[i].isdigit():\n",
    "                x += number[i]\n",
    "        count = len(x)\n",
    "        while count > 4:\n",
    "            target += x[:3] + \"-\"\n",
    "            count -= 3\n",
    "            x = x[3:]\n",
    "        if count == 4:\n",
    "            return target + x[:2] + \"-\" + x[2:]\n",
    "        else:\n",
    "            return target + x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        new_number=[]\n",
    "        for i in number:\n",
    "            if i !=' 'and i!='-':\n",
    "                new_number.append(i)\n",
    "\n",
    "        length=len(new_number)\n",
    "        i=0\n",
    "\n",
    "        output_number=''\n",
    "        left=length\n",
    "        while left>4:\n",
    "            for j in range(3):\n",
    "\n",
    "                output_number+=new_number[i]\n",
    "                i+=1\n",
    "                left-=1\n",
    "            output_number+='-'\n",
    "        if left==4:\n",
    "            output_number+=new_number[-4]\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+='-'\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==3:\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==2:\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        return output_number\n",
    "\n",
    "\n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        tmp = list(number.replace(' ',\"\").replace(\"-\", \"\"))\n",
    "        resList = []\n",
    "        res_tmp=\"\" \n",
    "        while True:\n",
    "            try:\n",
    "                res_tmp += tmp.pop(0)\n",
    "            except Exception:\n",
    "                if res_tmp:\n",
    "                    resList.append(res_tmp)\n",
    "                break\n",
    "            \n",
    "            if len(res_tmp)==2 and len(tmp)==2:\n",
    "                if res_tmp:\n",
    "                    resList.append(res_tmp)\n",
    "                res_tmp=\"\"            \n",
    "            \n",
    "            if len(res_tmp)==3:\n",
    "                if res_tmp:\n",
    "                    resList.append(res_tmp)\n",
    "                res_tmp=\"\"\n",
    "        \n",
    "        \n",
    "        \n",
    "        return \"-\".join(resList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:        \n",
    "        n3 = ''                             #存储纯数字的地方\n",
    "        number = number.replace(\" \",\"\").replace(\"-\",\"\")\n",
    "        l = len(number)                     #求字符串长度\n",
    "        m = l // 3                          #求能切几下\n",
    "        m2 = l % 3\n",
    "        if l > 3:\n",
    "            for j in range(0, m-1):\n",
    "                n3 += number[3 * j:(j + 1) * 3] + '-'\n",
    "        else:\n",
    "            return number\n",
    "        if m2 == 0:\n",
    "            n3 += number[3*(m-1):m*3]\n",
    "        elif m2 == 1:\n",
    "            n3 += number[-4:-2] + '-' + number[-2:]\n",
    "        elif m2 == 2:\n",
    "            n3 += number[-5:-2] + '-' + number[-2:]\n",
    "        return n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number1=''\n",
    "        for i in number:\n",
    "            if i!='-' and i!=' ':\n",
    "                number1+=i\n",
    "        n=len(number1)//3\n",
    "        if len(number1)%3==1:\n",
    "            n-=1\n",
    "        ans=''\n",
    "        for i in range(n):\n",
    "            ans+=number1[i*3:i*3+3]+'-'\n",
    "        if len(number1)%3==1:\n",
    "            ans+=number1[-4:-2]\n",
    "            ans+='-'\n",
    "            return ans+number1[len(number1)-2:len(number1)]\n",
    "        elif len(number1)%3==2:\n",
    "            return ans+number1[len(number1)-2:len(number1)]\n",
    "        return ans.strip('-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number=number.replace('-','')\n",
    "        number=number.replace(' ','')\n",
    "        n=len(number)\n",
    "        part=0\n",
    "        res=''\n",
    "        for i in range(n):\n",
    "            if part<2:\n",
    "                if part==1 and len(number[i+1:])==2:\n",
    "                    #print('if'+str(i))\n",
    "                    res+=number[i]+'-'+number[-2:]\n",
    "                    return res\n",
    "                else:\n",
    "                    res+=number[i]\n",
    "                part+=1\n",
    "            else:\n",
    "                part=0\n",
    "                if i==len(number)-1:\n",
    "                    res+=number[i]\n",
    "                else:\n",
    "                    res+=number[i]+'-'\n",
    "                #print('other'+str(i))\n",
    "           # print(res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        ans = ''\n",
    "        pos = 0\n",
    "        total = sum(1 for x in number if x !='-' and x !=' ')\n",
    "        need = 0\n",
    "        if total % 3 ==2:\n",
    "            need = total-2\n",
    "        elif total % 3 ==1:\n",
    "            need = total- 2\n",
    "        else:\n",
    "            need = total-3\n",
    "        for i in range(len(number)):\n",
    "            if number[i] != '-' and number[i] !=' ':\n",
    "                ans+=number[i]\n",
    "                pos+=1\n",
    "                if pos<need and pos % 3==0:\n",
    "                    ans += '-'\n",
    "                elif pos == need:\n",
    "                    ans += '-'\n",
    "        \n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\"-\", \"\").replace(\" \", \"\")\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3 : i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "\n",
    "        newNum = \"\"\n",
    "\n",
    "\n",
    "        for num in number:\n",
    "\n",
    "\n",
    "            if num.isdigit():\n",
    "                newNum += num\n",
    "\n",
    "        cnt = 0\n",
    "\n",
    "        res = \"\"\n",
    "\n",
    "        n = len(newNum)\n",
    "        print(newNum)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            left = n - i \n",
    "            if left > 4:\n",
    "                res += newNum[i: i + 3] + \"-\"\n",
    "            else:\n",
    "                break\n",
    "            i += 3\n",
    "        if left == 2 or left == 3:\n",
    "            res += newNum[i:]\n",
    "        elif left == 4:\n",
    "            res +=  newNum[i: i + 2] + \"-\" + newNum[i + 2:]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        # 删除所有的空格和破折号  \n",
    "        number = ''.join(c for c in number if c in '0123456789')  \n",
    "    \n",
    "        # 将数组从左到右每3个一组分块，直到剩下4个或更少数字  \n",
    "        chunks = [number[i:i+3] for i in range(0, len(number), 3)]  \n",
    "\n",
    "        k = len(chunks[-1]) % 3\n",
    "        # 根据规定再分块  \n",
    "        result = '-'.join(chunks)\n",
    "        return result[:-3] + \"-\" + result[-3] + result[-1] if k == 1 else result[:]  # 去掉第一个额外的破折号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace('-', '').replace(' ', '')\n",
    "        n = len(number)\n",
    "        ans = [number[i * 3:i * 3 + 3] for i in range(n // 3)]\n",
    "        if n % 3 == 1:\n",
    "            ans[-1] = ans[-1][0:2]\n",
    "            ans.append(number[-2:])\n",
    "        elif n % 3 == 2:\n",
    "            ans.append(number[-2:])\n",
    "        return '-'.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        new_number=[]\n",
    "        for i in number:\n",
    "            if i !=' 'and i!='-':\n",
    "                new_number.append(i)\n",
    "\n",
    "        length=len(new_number)\n",
    "        i=0\n",
    "\n",
    "        output_number=''\n",
    "        left=length\n",
    "        while left>4:\n",
    "            for j in range(3):\n",
    "\n",
    "                output_number+=new_number[i]\n",
    "                i+=1\n",
    "                left-=1\n",
    "            output_number+='-'\n",
    "        if left==4:\n",
    "            output_number+=new_number[-4]\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+='-'\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==3:\n",
    "            output_number+=new_number[-3]\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        elif left==2:\n",
    "            output_number+=new_number[-2]\n",
    "            output_number+=new_number[-1]\n",
    "        return output_number\n",
    "\n",
    "\n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        digits = list()\n",
    "        for ch in number:\n",
    "            if ch.isdigit():\n",
    "                digits.append(ch)\n",
    "        \n",
    "        n, pt = len(digits), 0\n",
    "        ans = list()\n",
    "\n",
    "        while n > 0:\n",
    "            if n > 4:\n",
    "                ans.append(\"\".join(digits[pt:pt+3]))\n",
    "                pt += 3\n",
    "                n -= 3\n",
    "            else:\n",
    "                if n == 4:\n",
    "                    ans.append(\"\".join(digits[pt:pt+2]))\n",
    "                    ans.append(\"\".join(digits[pt+2:pt+4]))\n",
    "                else:\n",
    "                    ans.append(\"\".join(digits[pt:pt+n]))\n",
    "                break\n",
    "        \n",
    "        return \"-\".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = \"\".join(c for c in number if c not in (' ', '-'))\n",
    "        idx, tail = None, ''\n",
    "        if len(number) % 3 == 1:\n",
    "            idx = -4\n",
    "            tail = number[-4:-2] + '-' + number[-2:]\n",
    "        elif len(number) % 3 == 2:\n",
    "            idx = -2\n",
    "            tail = number[-2:]\n",
    "\n",
    "        # print(number, idx, tail)\n",
    "\n",
    "        blocks = (b[0]+b[1]+b[2] for b in zip(*(number[i:idx:3] for i in range(3))))\n",
    "        head = \"-\".join(blocks)\n",
    "        return \"-\".join(part for part in (head, tail) if part)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        number = number.replace(\" \",\"\").replace(\"-\",\"\")\n",
    "        _len = len(number)\n",
    "        if _len < 4:\n",
    "            return number\n",
    "        new_number = \"\"\n",
    "        idx = 0\n",
    "        while _len > 4:\n",
    "            new_number += number[idx:idx+3]\n",
    "            new_number += \"-\"\n",
    "            idx += 3\n",
    "            _len -= 3\n",
    "        if _len == 4:\n",
    "            new_number += number[idx:idx+2]\n",
    "            new_number += \"-\"\n",
    "            new_number += number[idx+2:]\n",
    "        \n",
    "        if _len <= 3:\n",
    "            new_number += number[idx:]\n",
    "\n",
    "        return new_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, number: str) -> str:\n",
    "        teleno = ''\n",
    "        for l in number:\n",
    "            if l.isdigit():\n",
    "                teleno += l\n",
    "        #print(teleno)\n",
    "        result = ''\n",
    "        block3 = (len(teleno) - 2) // 3\n",
    "        for i in range(0,block3):\n",
    "            result += teleno[3*i : 3*i+3] + '-'\n",
    "        if len(teleno) - 3*block3 == 2:\n",
    "            result += teleno[-2:]\n",
    "        elif len(teleno) - 3*block3 == 3:\n",
    "            result += teleno[-3:]\n",
    "        elif len(teleno) - 3*block3 == 4:\n",
    "            result += teleno[-4:-2] + '-' + teleno[-2:]\n",
    "        return result\n",
    "\n",
    "\n",
    "# 123-45-67\n",
    "# 123-456-78\n",
    "# 123-456-789\n",
    "# 123-456-78-90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reformatNumber(self, s: str) -> str:\n",
    "        s = s.replace('-', '').replace(' ', '')\n",
    "        ret = ''\n",
    "        while len(s) > 4:\n",
    "            ret += s[:3] + '-'\n",
    "            s = s[3:]\n",
    "        if len(s) <= 3:\n",
    "            ret += s\n",
    "        else:\n",
    "            ret += s[:2] + '-' + s[2:]\n",
    "        return ret"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
